snippet db.tpl
	import Sequelize from 'sequelize'
	import dbConn from '../utils/db_conn'
	import uuid from 'node-uuid'

	console.log('init sequelize...')
	const ID_TYPE = Sequelize.STRING(50)

	export default {
		defineModel (name, attributes) {
			var attrs = {}
			for (let key in attributes) {
				let value = attributes[key]
				if (typeof value === 'object' && value['type']) {
					value.allowNull = value.allowNull || false
					attrs[key] = value
				} else {
					attrs[key] = {
						type: value,
						allowNull: false
					}
				}
			}

			attrs.id = {
				type: ID_TYPE,
				primaryKey: true
			}
			attrs.createdAt = {
				type: Sequelize.BIGINT,
				allowNull: false
			}
			attrs.updatedAt = {
				type: Sequelize.BIGINT,
				allowNull: false
			}
			attrs.deletedAt = {
				type: Sequelize.BIGINT,
				allowNull: true
			}
			attrs.version = {
				type: Sequelize.BIGINT,
				allowNull: false
			}
			attrs.status = {
				type: Sequelize.BOOLEAN,
				defaultValue: 0
			}

			return dbConn.define(name, attrs, {
				tableName: name,
				timestamps: false,
				hooks: {
					beforeValidate (obj) {
						let now = Date.now()
						if (obj.version || obj.version === 0) {
							obj.updatedAt = now
							obj.version++
						} else {
							obj.id = obj.id ? obj.id : uuid.v1()
							obj.createdAt = now
							obj.updatedAt = now
							obj.version = 0
						}
					}
				}
			})
		}
	}
